package com.softlab.retailwholesalesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softlab.retailwholesalesystem.entity.GoodsEntity;
import com.softlab.retailwholesalesystem.service.IGoodsService;
import com.softlab.retailwholesalesystem.service.exception.UpdateErrorException;
import com.softlab.retailwholesalesystem.util.JsonResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;

    @GetMapping
    public JsonResult<List<GoodsEntity>> getAll() {

        List<GoodsEntity> goodsEntities = goodsService.getAll();

        return new JsonResult<>(200, "货物列表查询成功", goodsEntities);
    }

    @PostMapping
    public JsonResult<Void> addGood(@RequestBody GoodsEntity goods) {
        goodsService.save(goods);
        return new JsonResult<>(200, "货物增加成功");
    }


    @PutMapping
    public JsonResult<Void> changeGood(@RequestBody GoodsEntity goods) {
        boolean b = goodsService.updateById(goods);
        if (!b) throw new UpdateErrorException("修改时遇到错误");
        return new JsonResult<>(200, "货物修改成功");
    }

    @DeleteMapping("{id}")
    public JsonResult<Void> deleteGood(@PathVariable Long id) {
        GoodsEntity goods = new GoodsEntity();
        goods.setGoodsId(id);
        goods.setIsDelete(1);
        goodsService.updateById(goods);
        return new JsonResult<>(200, "货物删除成功");
    }

    @GetMapping("/search")
    public JsonResult<List<GoodsEntity>> getGoodById(GoodsEntity goods) {
        List<GoodsEntity> list = goodsService.searchGoods(goods);
        return new JsonResult<>(200, "货物查找成功", list);
    }

}
